package cn.com.elite.httpclient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;

import cn.com.elite.mail.MailSend;

public class HttpConnection {
	private static final Log log=LogFactory.getLog(HttpConnection.class);
	
	public String getConnectionResponse(String url,List params)throws Exception{
		String response="";
		HttpParams httpParameters = new BasicHttpParams();
		httpParameters.setBooleanParameter("http.protocol.expect-continue", false);
		HttpClient hc=new DefaultHttpClient(httpParameters);
		hc.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);
		HttpPost httpPost=new HttpPost(url); 
		httpPost.addHeader("Connection", "close");
	    httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); 
	    HttpResponse ht=hc.execute(httpPost);
	    if(ht.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
	    	HttpEntity het=ht.getEntity();  
            InputStream is=het.getContent();  
            BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));  
            String readLine=null;  
            while((readLine =br.readLine())!=null){  
                response=response+readLine;  
            }  
            is.close();  
            br.close();
	    }else{
	    	log.error("ht.getStatusLine().getStatusCode():"+ht.getStatusLine().getStatusCode());
	    }
        log.debug("response:"+response);
		return response;
	}

}
